package server.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.PropertyConfigurator;

import server.config.ServerConfig;
import server.db.ConnectionPool;
import server.db.SQL;

public class RemoveDebugFromDB {

	
	public static void main(String[] args) {
		
		PropertyConfigurator.configure("log4J.cfg");
		try {
			new ServerConfig("serverConfigLocal.cfg");
			
		} catch (IOException e) {
		}
		SQL.getInstance();
		
		SQL.removeDebugAlarme();
		
		System.out.println("Fertig Teil 1");
		
		
		PreparedStatement stmt;
		Connection con = null;
		ConnectionPool conPool = SQL.getInstance().getConPool();
		try {
			con = conPool.getConnectionFromPool();			
			stmt = con.prepareStatement("SELECT alarmid, id FROM  `alarmierteschleifen`");

			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {

				PreparedStatement stmt2;
				stmt2 = con.prepareStatement("SELECT id FROM `alarmierungen` WHERE id = ?");
				stmt2.setInt(1, rs.getInt("alarmid"));
				ResultSet rs2 = stmt2.executeQuery();
				boolean ok = false;
				while (rs2.next()) {
					ok = true;
					
				}
				
				if(!ok) {
					System.out.println(rs.getInt("alarmid"));
					System.out.println(rs.getInt("id"));
					PreparedStatement stmt3;
					stmt3 = con.prepareStatement("DELETE FROM  `alarmierteschleifen` WHERE alarmid = ?");
					stmt3.setInt(1, rs.getInt("alarmid"));
					stmt3.executeUpdate();
				}
				
				
				stmt2.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			conPool.returnConnectionToPool(con);
		}
		
		System.out.println("Fertig Teil2");
	}
}
